from django.shortcuts import render

# Create your views here.

# 轮播图接口
from rest_framework.viewsets import ViewSetMixin
from rest_framework.generics import ListAPIView
from .serializer import BannerSerializer
from .models import Banner
from django.conf import settings
from django.core.cache import cache
from rest_framework.response import Response


class BannerView(ViewSetMixin, ListAPIView):
    queryset = Banner.objects.all().filter(is_show=True, is_delete=False).order_by('-orders')[0:settings.BANNER_SIZE]
    serializer_class = BannerSerializer
    def list(self, request, *args, **kwargs):
        banner_list = cache.get('banner_cache_list')
        if not banner_list:  # 没有值，去数据库查
            response = super().list(request, *args, **kwargs)
            # 在缓存中放一份
            cache.set('banner_cache_list', response.data)
        else:
            print('走了缓存')
            response = Response(data=banner_list)
        return response
